package com.example.demo.aop;

import com.example.demo.annotation.FilterTest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AspectJTest {

    @Pointcut("@annotation(com.example.demo.annotation.FilterTest)")
    public void pointCut(){

    }

//    @Before("pointCut()")
//    public void Before(JoinPoint joinPoint){
//        if(joinPoint.getArgs()!=null){
//            String param =  (String) joinPoint.getArgs()[0];
//            System.out.println("arg is "+ param);
//        }
//
//        System.out.println("this is before");
//    }
//
//    @After("pointCut()")
//    public void After(){
//        System.out.println("this is after");
//    }

    @Around("pointCut()")
    public Object Around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("this is around");
        String args = (String) joinPoint.getArgs()[0];
        System.out.println(args);
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        System.out.println(className);
        System.out.println(methodName);
        //获取自定义注解的参数
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        FilterTest filterTest = methodSignature.getMethod().getAnnotation(FilterTest.class);
        System.out.println(filterTest.param1()+">>>>");
        String type =  methodSignature.getReturnType().getName();
        System.out.println(type+"name");
        Object[] ars = new Object[]{"asd"};
        Object result =  joinPoint.proceed(ars);
        return result;
    }

}
